﻿// -----------------------------------------------------------------------
// <copyright file="Vendor_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Vendor_DAL:BaseDAL
    {

        // Lấy tất cả Vendor
        public static List<Vendor> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Vendors);
            return DB.Vendors.Where(n=>n.Ship_ID == shipID).ToList();
        }
        // Lấy Vendor theo Id
        public static Vendor GetDataById(int id)
        {
            return DB.Vendors.Where(v => v.Vendor_ID == id).FirstOrDefault();
        }
        public static Vendor GetDataByCode(string code, int shipID)
        {
            return DB.Vendors.Where(m => m.CodeVendor == code && m.Ship_ID == shipID).FirstOrDefault();
        }
        // Lấy Maker theo Id
        public static Vendor GetDataByName(string name, int shipID)
        {
            return DB.Vendors.Where(m => m.NameVendor == name && m.Ship_ID == shipID).FirstOrDefault();
        }
        //Thêm một Vendor
        public static bool Insert(Vendor vendor)
        {
            try
            {
                DB.Vendors.InsertOnSubmit(vendor);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Vendor
        public static bool Delete(Vendor vendor)
        {
            try
            {
                DB.Vendors.DeleteOnSubmit(vendor);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Vendor
        public static bool Update(Vendor vendor)
        {
            try
            {
                //Vendor objVendor = GetDataById(vendor.Vendor_ID);
                //objVendor.CodeVendor = vendor.CodeVendor;
                //objVendor.NameVendor = vendor.NameVendor;
                //objVendor.Address = vendor.Address;
                //objVendor.Tell = vendor.Tell;
                //objVendor.Fax = vendor.Fax;
                //objVendor.Email = vendor.Email;
                //objVendor.ContactPerson = vendor.ContactPerson;
                //objVendor.Note = vendor.Note;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
      
        // Kiểm tra xem Vendor Name có tồn tại chưa ?
        public static bool IsExits(Vendor vendor)
        {
            Vendor objVendor = null;
            objVendor = DB.Vendors.Where(m => (m.CodeVendor == vendor.CodeVendor) &&
                (m.Vendor_ID != vendor.Vendor_ID) && (m.Ship_ID == vendor.Ship_ID)).FirstOrDefault();
            return objVendor != null ? true : false;
        }
        public static bool IsRelationship(Vendor vendor)
        {
            Equipment equipment = DB.Equipments.Where(n => n.Vendor_ID == vendor.Vendor_ID).FirstOrDefault();
            if (null != equipment)
                return false;

            PartsInEquipment part = DB.PartsInEquipments.Where(n => n.Vendor_ID == vendor.Vendor_ID).FirstOrDefault();
            if (null != part)
                return false;

            return true;
        }


    }
}
